package com.yanqu.road.server.cmd.mall;

import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * p_game_id	number	固定传：118	是	118
 * login_account	String(50)	平台UID	是
 * sid	number	区服ID	是	0
 * pay_time	timestamp	使用通宝兑换的时间，10位时间戳
 * 示例:1694155383	是	0
 * pay_money	double	使用通宝的数量	是	0.00
 * product_id	String(50)	使用通宝兑换的商品ID	是	0
 * product_name	String(255)	使用通宝兑换的商品名称	是
 * order_id	String(255)	单次兑换生成的订单号，可以是虚拟的，需要保证唯一	是
 * order_type	String(59)	订单类型，1为宫廷充值订单，2为掌柜通宝兑换
 * 固定传：2	是	2
 */
public class TokenBuyReportTask extends GeneralTask {
    private Logger logger = LogManager.getLogger(TokenBuyReportTask.class.getName());

    private String platformUserId;              //平台账户id

    private long serverId;

    private String channelId;           //大渠道

    private String subChannelId;        //子渠道

    private long time;

    private long tokenNum;

    private long mallMarkId;

    private String mallName;

    private String orderId;

    public TokenBuyReportTask(String platformUserId, long serverId, String channelId, String subChannelId, long time, long tokenNum, long mallMarkId, String mallName, String orderId) {
        this.platformUserId = platformUserId;
        this.serverId = serverId;
        this.channelId = channelId;
        this.subChannelId = subChannelId;
        this.time = time;
        this.tokenNum = tokenNum;
        this.mallMarkId = mallMarkId;
        this.mallName = mallName;
        this.orderId = orderId;
    }


    @Override
    public void doRun() {
        String url = ChannelConfig.TOKEN_BUY_REPORT_URL;
        if (StringUtils.isNullOrEmpty(url)){
            return;
        }
        orderId =platformUserId+""+serverId+""+time;

        SortedMap<String, String> paramMap = new TreeMap<>();
        paramMap.put("p_game_id","118");
        paramMap.put("login_account",platformUserId);
        paramMap.put("sid",String.valueOf(serverId));
        paramMap.put("pay_time",String.valueOf(time/1000));
        paramMap.put("pay_money",String.valueOf(tokenNum));
        paramMap.put("product_id",String.valueOf(mallMarkId));
        paramMap.put("product_name",mallName);
        paramMap.put("order_id",orderId);
        paramMap.put("order_type","2");//订单类型，1为宫廷充值订单，2为掌柜通宝兑 换固定传：2

        try {
            Map<String, String> resultMap = HttpHelper.doGetWithoutQuestionMark(url, paramMap);
            if(null == resultMap){
                logger.info("token buy Report request error");
            }else {
                logger.info("token buy Report code:{}; result:{}", resultMap.get("code"), resultMap.get("body"));
            }
        }catch (Exception e){
            logger.error("token buy Report error:",e);
        }

    }
}
